<?php

    function richTextAbsoluteUrl($html_content, $host)
    {
        if (preg_match_all("/(<img[^>]+src=\"([^\"]+)\"[^>]*>)|(<a[^>]+href=\"([^\"]+)\"[^>]*>)|(<img[^>]+src='([^']+)'[^>]*>)|(<a[^>]+href='([^']+)'[^>]*>)/i", $html_content, $regs)) {
            foreach ($regs [0] as $num => $url) {
                $html_content = str_replace($url, lIIIIl($url, $host), $html_content);
            }
        }
        return $html_content;
    }

    function lIIIIl($l1, $l2)
    {
        if (preg_match("/(.*)(href|src)\=(.+?)( |\/\>|\>).*/i", $l1, $regs)) {
            $I2 = $regs [3];
        }
        if (strlen($I2) > 0) {
            $I1 = str_replace(chr(34), "", $I2);
            $I1 = str_replace(chr(39), "", $I1);
        } else {
            return $l1;
        }
        $url_parsed = parse_url($l2);
        $scheme = isset($url_parsed['scheme']) ? $url_parsed ["scheme"] : '';
        if ($scheme != "") {
            $scheme = $scheme . "://";
        }
        $host = isset($url_parsed ["host"]) ? $url_parsed['host'] : '';
        $l3 = $scheme . $host;
        if (strlen($l3) == 0) {
            return $l1;
        }
        $path = isset($url_parsed ["path"]) ? dirname($url_parsed ["path"]) : '' ;
        if(!empty($path)){
            if ($path [0] == "\\") {
                $path = "";
            }
        }
        $pos = strpos($I1, "#");
        if ($pos > 0)
            $I1 = substr($I1, 0, $pos);

        //判断类型
        if (preg_match("/^(http|https|ftp):(\/\/|\\\\)(([\w\/\\\+\-~`@:%])+\.)+([\w\/\\\.\=\?\+\-~`@\':!%#]|(&amp;)|&)+/i", $I1)) {
            return $l1;
        } //http开头的url类型要跳过
        elseif ($I1 [0] == "/") {
            $I1 = $l3 . $I1;
        } //绝对路径
        elseif (substr($I1, 0, 3) == "../") { //相对路径
            while (substr($I1, 0, 3) == "../") {
                $I1 = substr($I1, strlen($I1) - (strlen($I1) - 3), strlen($I1) - 3);
                if (strlen($path) > 0) {
                    $path = dirname($path);
                }
            }
            $I1 = $l3 . $path . "/" . $I1;
        } elseif (substr($I1, 0, 2) == "./") {
            $I1 = $l3 . $path . substr($I1, strlen($I1) - (strlen($I1) - 1), strlen($I1) - 1);
        } elseif (strtolower(substr($I1, 0, 7)) == "mailto:" || strtolower(substr($I1, 0, 11)) == "javascript:") {
            return $l1;
        } else {
            $I1 = $l3 . $path . "/" . $I1;
        }
        return str_replace($I2, "\"$I1\"", $l1);
    }

    /**
     *  富文本绝对路径替换成相对路径
     * @param $html_content
     * @param $host
     * @return mixed
     */
    function richTextRelativeUrl($html_content, $host)
    {
        return str_replace($host, '', $html_content);
    }
    
    
    function myTrim($str)
    {
        $search = array(" ","　","\n","\r","\t");
        $replace = array("","","","","");
        return str_replace($search, $replace, $str);
    }
    
    
    function getOrderNo()
    {
        //生成24位唯一订单号码，格式：YYYY-MMDD-HHII-SS-NNNN,NNNN-CC，其中：YYYY=年份，MM=月份，DD=日期，HH=24格式小时，II=分，SS=秒，NNNNNNNN=随机数，CC=检查码
        
        @date_default_timezone_set("PRC");
        //订购日期
        
        $order_date = date('Y-m-d');
        
        //订单号码主体（YYYYMMDDHHIISSNNNNNNNN）
        
        $order_id_main = date('YmdHis') . rand(10000000,99999999);
        
        //订单号码主体长度
        
        $order_id_len = strlen($order_id_main);
        
        $order_id_sum = 0;
        
        for($i=0; $i<$order_id_len; $i++){
            
            $order_id_sum += (int)(substr($order_id_main,$i,1));
            
        }
        
        //唯一订单号码（YYYYMMDDHHIISSNNNNNNNNCC）
        
        $order_id = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT);
        return $order_id;
    }
    /**
     * 发送post请求
     * @param string $url 请求地址
     * @param array $post_data post键值对数据
     * @return string
     */
    function send_post($url, $post_data) {
        $postdata = http_build_query($post_data);
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:application/x-www-form-urlencoded',
                'content' => $postdata,
                'timeout' => 15 * 60 // 超时时间（单位:s）
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return $result;
    }
    
    //自定义函数手机号隐藏中间四位
    function yc_phone($str){
        $resstr=substr_replace($str,'****',3,4);
        return $resstr;
    }
	
	/**
	 * 生成签名结果
	 * @param $para_sort 已排序要签名的数组
	 * return 签名结果字符串
	 */
	function buildRequestMysign($para_sort) {
		//把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
		$prestr = createLinkstring($para_sort);
		
		$mysign = md5Sign($prestr, alipay_config['key']);
	
		return $mysign;
	}
	/**
	 * 生成要请求给支付宝的参数数组
	 * @param $para_temp 请求前的参数数组
	 * @return 要请求的参数数组
	 */
	function buildRequestPara($para_temp) {
		//除去待签名参数数组中的空值和签名参数
		$para_filter = paraFilter($para_temp);
	
		//对待签名参数数组排序
		$para_sort = argSort($para_filter);
	
		//生成签名结果
		$mysign = buildRequestMysign($para_sort);
		
		//签名结果与签名方式加入请求提交参数组中
		$para_sort['sign'] = $mysign;
		$para_sort['sign_type'] = strtoupper(trim(alipay_config['sign_type']));
		
		return $para_sort;
	}
	/**
	 * 生成要请求给支付宝的参数数组
	 * @param $para_temp 请求前的参数数组
	 * @return 要请求的参数数组字符串
	 */
	function buildRequestParaToString($para_temp) {
		//待请求参数数组
		$para = buildRequestPara($para_temp);
		
		//把参数组中所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串，并对字符串做urlencode编码
		$request_data = createLinkstringUrlencode($para);
		
		return $request_data;
	}
	/**
	 * 建立请求，以URL形式构造
	 * @param $para_temp 请求参数数组
	 * @return 提交的URL链接
	 */
	function buildRequestUrl($para_temp) {		
		//待请求参数数组字符串
		$request_data = buildRequestPara($para_temp);
	
		//远程获取数据
		$url = alipay_qrcode.http_build_query($request_data);
	
		return $url;
	}
	/**
	 * 建立请求，以表单HTML形式构造（默认）
	 * @param $para_temp 请求参数数组
	 * @param $method 提交方式。两个值可选：post、get
	 * @param $button_name 确认按钮显示文字
	 * @return 提交表单HTML文本
	 */
	function buildRequestForm($para_temp, $method='POST', $button_name='正在跳转') {
		//待请求参数数组
		$para = buildRequestPara($para_temp);
		
		$sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='".alipay_gateway_new."' method='".$method."'>";
		foreach ($para as $key=>$val) {
	        $sHtml.= "<input type='hidden' name='".$key."' value='".$val."'/>";
	    }
	
		//submit按钮控件请不要含有name属性
	    $sHtml = $sHtml."<input type='submit' value='".$button_name."'></form>";
		
		$sHtml = $sHtml."<script>document.forms['alipaysubmit'].submit();</script>";
		
		return $sHtml;
	}
	function __construct($alipay_config){
		$this->alipay_config = $alipay_config;
		$this->alipay_gateway_new = $this->alipay_config['apiurl'].'submit.php';
		$this->alipay_qrcode = $this->alipay_config['apiurl'].'qrcode.php?';
	}
	function AlipaySubmit($alipay_config) {
		__construct($alipay_config);
	}